<?php
require_once('twitteroauth.php');
require_once('config.php');

if (ACCESS_TOKEN_SECRET === '' || ACCESS_TOKEN === '') {
    header('Location: connect.php');
}
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

$blacklist=array('求fo','fo我','法轮功','三代','胡锦涛','共产党','baozuitun','http','@','#','test','helloworld','hello world');

$twittercmd=array('follow ','fav ','d ');

function post($msg) {
	global $connection,$blacklist,$twittercmd,$_COOKIE,$_SESSION;
	$msg = ltrim($msg);
	if ($_COOKIE["spaming"] === "yes") {
		echo "你已经被禁止发言";
		return NULL;
	}
	if ($_SESSION["ptimes"]>5) {
		echo "有点累了吧？关掉浏览器去放松一下吧";
		return NULL;
	}
	if (mb_strlen($msg,'UTF-8')<3) {
		echo "我相信你还可以写得更多一点。";
		return NULL;
	}
	foreach ($blacklist as $ing) {
		$msg2 = str_ireplace("@rtmimi","",$msg);
		if (!(stripos($msg2,$ing)===false)) {
			setcookie("spaming","yes",time()+1800);
			echo "包含敏感词，你已经被禁止发言";
			return NULL;
		}
	}
	foreach ($twittercmd as $ing) {
		if (stripos($msg,$ing)===0) {
			setcookie("spaming","yes",time()+1800);
			echo "不要滥用本id";
			return NULL;
		}
	}
	$aa = array('status' => $msg);
	if (strlen($_POST['r'])>2) $aa['in_reply_to_status_id']=$_POST['r'];
	$rtn = $connection->post('statuses/update', $aa);
	if ($rtn != NULL) {
		$_SESSION["ptimes"]++;
		$msg1 = str_replace("&","&amp;",$msg);
		$msg1 = str_replace(">","&gt;",$msg);
		$msg1 = str_replace("<","&lt;",$msg);
		$msg1 = str_replace("\n","<br>",$msg1);
		file_put_contents("cache1.txt","\n{$rtn->id};{$_POST['r']};{$rtn->created_at};{$msg1}",FILE_APPEND);
	}
	return $rtn;
}

function del($id){
	global $connection;
	return $connection->post('statuses/destroy', array('id' => strval($id)));
}

//$connection->get('statuses/user_timeline', array());
//$connection->get('users/show', array('screen_name' => 'abraham'));
//$connection->post('statuses/destroy', array('id' => 5437877770));
//$connection->post('friendships/create', array('id' => 9436992)));
//$connection->post('friendships/destroy', array('id' => 9436992));

// http://apiwiki.twitter.com/Twitter-API-Documentation

session_start();
if (!isset($_SESSION["me"])) 
	$_SESSION["me"] = $connection->get('account/verify_credentials', array());
define("TheID",$_SESSION["me"]->screen_name);
if (!isset($_SESSION["ptimes"])) 
	$_SESSION["ptimes"] = 0;
?>